package sklad;

import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;
import jxl.write.*;
import jxl.write.Number;
import obj.Item;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.*;

/**
 * User: Vladimir
 * Date: 06.10.12 Time: 23:11
 */
public class PriceHelper {

    public static final String CURR_PRICES_FILE = "G:\\projects\\my-im-tools\\sklad\\текущие цены.xls";
    public static final String NEW_PRICES_FILE = "G:\\projects\\my-im-tools\\sklad\\новые цены - extamobi.xls";

    public static void main(String[] args) throws BiffException, IOException, ParseException, WriteException {
        String inPath = "G:\\projects\\my-im-tools\\sklad\\in-bills\\";
//        List<GoodItem> inItems = Parser.parseDir(inPath);
//        saveCurrentPrices(inItems);
        List<GoodItem> currItems = readCurrentPrices();
        for (GoodItem item : currItems) {
            System.out.println(item);
        }
        savePricesChange(currItems);
    }

    private static void savePricesChange(List<GoodItem> currItems) throws BiffException, IOException, WriteException {
//        Workbook workbook = Workbook.getWorkbook(new File(CURR_PRICES_FILE));
//        Sheet sheet = workbook.getSheet(0);
        List<Item> items = Item.parseAllItemsDir("data/extramobi/");
        for (Item item : items) {
            System.out.println(item);
        }

        List<GoodItem> itemsChanged = new ArrayList<GoodItem>();

        for (GoodItem currItem : currItems) {
            Item item = getItem(items, currItem.article);
            if (item.price != currItem.sellPrice) {
                GoodItem newItem = GoodItem.copyItem(currItem);
                newItem.sellPrice = item.price;
                itemsChanged.add(newItem);
            }
        }
        Collections.sort(itemsChanged, new Comparator<GoodItem>() {
            @Override
            public int compare(GoodItem o1, GoodItem o2) {
                return o1.article.compareTo(o2.article);
            }
        });
        saveToXls(itemsChanged, currItems, NEW_PRICES_FILE);
    }

    private static Item getItem(List<Item> items, String article) {
        for (Item item : items) {
            if (item.article.equals(article)) {
                return item;
            }
        }
        throw new IllegalArgumentException("Article " + article + " not found.");
    }

    private static List<GoodItem> readCurrentPrices() throws BiffException, IOException {
        Workbook workbook = Workbook.getWorkbook(new File(CURR_PRICES_FILE));
        Sheet sheet = workbook.getSheet(0);
        List<GoodItem> items = new ArrayList<GoodItem>();
        for (int row = 1; row < sheet.getRows(); row++) {
            GoodItem item = new GoodItem();
            item.article = sheet.getCell(1, row).getContents();
            item.name = sheet.getCell(2, row).getContents();
            item.sellPrice = Double.parseDouble(sheet.getCell(3, row).getContents());
            items.add(item);
        }
        return items;
    }

    private static void saveCurrentPrices(List<GoodItem> inItems) throws BiffException, IOException, WriteException {
        List<GoodItem> currItems = new ArrayList<GoodItem>();
        List<Item> items = Item.parseAllItemsDir("data/extramobi/");
        for (Item item : items) {
            GoodItem goodItem = new GoodItem();
            goodItem.article = item.article;
            if (inItems.contains(goodItem)) {
                goodItem = inItems.get(inItems.indexOf(goodItem));
                goodItem.count = 0;
                goodItem.sellPrice = item.price;
                currItems.add(goodItem);
            }
        }
        Collections.sort(currItems, new Comparator<GoodItem>() {
            @Override
            public int compare(GoodItem o1, GoodItem o2) {
                return o1.article.compareTo(o2.article);
            }
        });
        saveToXls(currItems, inItems, CURR_PRICES_FILE);
    }

    public static void saveToXls(List<GoodItem> outItems, List<GoodItem> oldItems, String xlsFile) throws IOException, WriteException {
        WorkbookSettings s = new WorkbookSettings();
        s.setLocale(new Locale("ru", "RU"));
        s.setEncoding("KOI8-R");
        WritableWorkbook workbook = Workbook.createWorkbook(new File(xlsFile), s);
        workbook.createSheet("new price", 0);
        WritableSheet sheet = workbook.getSheet(0);

        WritableCell cell = new Label(0, 0, "Номер");
        sheet.addCell(cell);
        cell = new Label(1, 0, "Артикул");
        sheet.addCell(cell);
        cell = new Label(2, 0, "Название");
        sheet.addCell(cell);
        cell = new Label(3, 0, "Старая цена");
        sheet.addCell(cell);
        cell = new Label(4, 0, "Новая цена");
        sheet.addCell(cell);

        int row = 1;
        for (GoodItem item : outItems) {
            cell = new Number(0, row, row);
            sheet.addCell(cell);
            cell = new Label(1, row, item.article);
            sheet.addCell(cell);
            cell = new Label(2, row, item.name);
            sheet.addCell(cell);
            cell = new Number(3, row, oldItems.get(oldItems.indexOf(item)).sellPrice);
            sheet.addCell(cell);
            cell = new Number(4, row, item.sellPrice);
            sheet.addCell(cell);
            row++;
        }
        workbook.write();
        workbook.close();
    }
}
